package com.shujia.sql

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment

object Demo8ToHbase {
  def main(args: Array[String]): Unit = {

    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //使用blink的计划器
      .inStreamingMode() //使用流模型
      .build()

    //窗口table 环境
    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)


    /**
      * source 表
      *
      */
    bsTableEnv.executeSql(
      """
        |
        |CREATE TABLE canal_student (
        | id STRING,
        | name STRING,
        | age BIGINT,
        | gender STRING,
        | clazz STRING
        |) WITH (
        | 'connector' = 'kafka',
        | 'topic' = 'student.student',
        | 'properties.bootstrap.servers' = 'master:9092',
        | 'properties.group.id' = 'asdasdasd',
        | 'format' = 'canal-json',
        | 'scan.startup.mode' = 'earliest-offset',
        | 'canal-json.ignore-parse-errors'='true'
        |)
        |
      """.stripMargin)


    /**
      * hbase sink表
      * 需要现在hbase中创建表
      * create 'student','info'
      *
      */

    bsTableEnv.executeSql(
      """
        |
        |CREATE TABLE hbase_sink (
        | id STRING,
        | info ROW<name STRING,age BIGINT,gender STRING,clazz STRING>,
        | PRIMARY KEY (id) NOT ENFORCED
        |) WITH (
        | 'connector' = 'hbase-1.4',
        | 'table-name' = 'student',
        | 'zookeeper.quorum' = 'master:2181'
        |)
        |
        |
      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |
        |insert into hbase_sink
        |select id,ROW(name,age,gender,clazz) as info from canal_student
        |
      """.stripMargin)



  }

}
